package com.chenl.provider.DEMOS.设计模式.策略模式.two;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class AnimalContextTwo {
    public static void main(String[] args) {
        List<String> list=new ArrayList<>();
        list.add("123");
        list.add("223");
        list.add("323");
        list.add("323");
        System.out.println(list.stream().distinct().collect(Collectors.toList()));
    }
    @Autowired
    AnimalService animalService;

    private final Map<Integer, Function<String,String>> map=new HashMap<>();
//    private final Map<Integer, Consumer<String>> map2=new HashMap<>();
//    private final Map<Integer, Supplier<String>> map3=new HashMap<>();

    //Spring启动时将对应参数执行不同方法的对应关系丢到Map中,使用函数式接口
    @PostConstruct
    public void setAnimalAction(){
        map.put(1,param->animalService.personEat(param));
        map.put(2,param->animalService.catEat(param));
        map.put(3,param->animalService.dogEat(param));
    }

    public String AnimalEat(Integer code,String a){
        Function<String, String> function = map.get(code);
        String result = function.apply(a);//函数式接口接收参数,执行函数体内容
        return result;
    }
}
